namespace("mk");
mk.keysManager = {
	keys:{}
    , listeners:{}
	, init: function() {
		var context = this;
					  
		$(document).bind("keydown", function(event) {
			switch (event.keyCode) {
			case 37:
                context.onEvent("keydown", "left");
				break;  
			case 39:
                context.onEvent("keydown", "right");
                break;
            case 38:
                context.onEvent("keydown", "top");
                    break;
            case 40:
                context.onEvent("keydown", "down");
                    break;
			case 17:
                context.onEvent("keydown", "ctrl");
				break;  	
			}
		});
			  
		$(document).bind("keyup", function(event) {
			switch (event.keyCode) {
			case 37:
                context.onEvent("keyup", "left");
				break;
			case 39:
                context.onEvent("keyup", "right");
				break;
                case 38:
                    context.onEvent("keyup", "top");
                    break;
                case 40:
                    context.onEvent("keyup", "down");
                    break;
            }
			
		});
	},
    addEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName] = clback;
    },
    removeEventListener: function(eventName, clback) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        this.listeners[eventName].splice(this.listeners[eventName].indexOf(clback), 1);
    },
    onEvent: function(eventName, e) {
        if(!this.listeners[eventName]) this.listeners[eventName] = [];
        for(var i=0; i<this.listeners[eventName].length; i++) {
            this.listeners[eventName][i](e);
        }
    }
};